const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const NotificationLog = sequelize.define('NotificationLog', {
  id: {
    type: DataTypes.BIGINT.UNSIGNED,
    primaryKey: true,
    autoIncrement: true,
    comment: '通知日志ID'
  },
  userId: {
    type: DataTypes.BIGINT.UNSIGNED,
    allowNull: false,
    field: 'user_id',
    comment: '用户ID'
  },
  messageId: {
    type: DataTypes.BIGINT.UNSIGNED,
    allowNull: true,
    field: 'message_id',
    comment: '消息ID'
  },
  type: {
    type: DataTypes.ENUM('wechat_template', 'sms', 'in_app'),
    allowNull: false,
    comment: '通知类型'
  },
  platform: {
    type: DataTypes.ENUM('miniprogram', 'h5', 'both'),
    allowNull: false,
    comment: '平台'
  },
  templateId: {
    type: DataTypes.STRING(100),
    allowNull: true,
    field: 'template_id',
    comment: '模板ID'
  },
  recipient: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '接收者（openid或手机号）'
  },
  title: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '通知标题'
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '通知内容'
  },
  templateData: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'template_data',
    comment: '模板数据'
  },
  status: {
    type: DataTypes.ENUM('pending', 'sent', 'failed', 'retry'),
    defaultValue: 'pending',
    comment: '发送状态'
  },
  errorMessage: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'error_message',
    comment: '错误信息'
  },
  retryCount: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    field: 'retry_count',
    comment: '重试次数'
  },
  sentAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'sent_at',
    comment: '发送时间'
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    field: 'created_at',
    comment: '创建时间'
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    field: 'updated_at',
    comment: '更新时间'
  }
}, {
  tableName: 'notification_logs',
  timestamps: false,
  comment: '通知日志表'
})

  return NotificationLog
}